package com.nkzy.mapper;

import com.nkzy.pojo.Address;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
public interface AddressMapper {
    @Insert("INSERT INTO address(user_id, province, city, town, mobile, street, contact, is_default, notes, deleted, create_time, update_time) " +
            "VALUES(#{userId}, #{province}, #{city}, #{town}, #{mobile}, #{street}, #{contact}, #{isDefault}, #{notes}, #{deleted}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertAddress(Address address);

    @Delete("DELETE FROM address WHERE id = #{id}")
    void deleteAddress(Long id);

    @Update("UPDATE address SET user_id = #{userId}, province = #{province}, city = #{city}, town = #{town}, " +
            "mobile = #{mobile}, street = #{street}, contact = #{contact}, is_default = #{isDefault}, notes = #{notes}, " +
            "deleted = #{deleted}, update_time = #{updateTime} WHERE id = #{id}")
    void updateAddress(Address address);

    @Select("SELECT * FROM address WHERE id = #{id}")
    Address selectAddressById(Long id);

    @Select("SELECT * FROM address WHERE user_id = #{userId}")
    List<Address> selectAddressesByUserId(Long userId);

    @Select("SELECT * FROM address")
    List<Address> selectAllAddresses();
}